//请求
import { reqCartList, reqCartDelete, reqcartedit } from "../../request/api";
//计算属性使用
import { createSelector } from "reselect"

//初始值
const initState = {
    //接收购物车的数据
    cartList: [],
    //定义开关 表示是否请求
    isReq: true,
    //定义全选
    checkedAll: false,
};

//types
const types = {
    //修改cartList
    CHANGE_CARTLIST: "SHOP_CHANGE_CARTLIST",
    //修改开关
    CHANGE_ISREQ: "SHOP_CHANGE_ISREQ",
    //修改全选
    CHANGE_CHECKEDALL: "SHOP_CHANGE_CHECKEDALL",
    //修改每一个小的checked
    CHANGE_CHECKEDONE: "SHOP_CHANGE_CHECKEDONE"
};

//修改数据
const reducer = (state = initState, action) => {
    switch (action.type) {
        //修改购物车数据
        case types.CHANGE_CARTLIST:
            return {
                ...state,
                cartList: action.arr
            }
        //修改开关
        case types.CHANGE_ISREQ:
            return {
                ...state,
                isReq: action.info
            }
        //修改全选
        case types.CHANGE_CHECKEDALL:
            //遍历数组  将全选的true或false 赋值给每一个小的checked
            state.cartList.map(ele => ele.checked = !state.checkedAll);
            return {
                ...state,
                cartList:[...state.cartList],
                //取反
                checkedAll: !action.info
            }
        //每一个小的checked
        case types.CHANGE_CHECKEDONE:
            //拿到当前数组
            let arr = state.cartList;
            //进行循环 然后找出点击的那一项  赋值checked为取反的自己
            arr.map(ele => {
                if (ele.id === action.id) {
                    ele.checked = !ele.checked;
                }
            })
            //全选赋值  循环原数组 
            state.checkedAll = arr.every(ele => ele.checked);
            return {
                ...state,
                cartList: [...arr]
            }
        default:
            return state;
    }
};

//组件派发的任务
export const actions = {
    //修改cartlist
    changeCartList: (arr) => ({ type: types.CHANGE_CARTLIST, arr }),
    //修改开关
    changeIsReq: (info) => ({ type: types.CHANGE_ISREQ, info }),
    //修改全选
    changeCheckedAll: (info) => ({ type: types.CHANGE_CHECKEDALL, info }),
    //修改小的checked
    changeCheckedOne: (id) => ({ type: types.CHANGE_CHECKEDONE, id }),
    //进行请求购物车数据
    reqCartList: () => (dispatch, getSate) => {
        //获取uid
        let { uid } = getSate().login.info
        let { cartList } = getSate().shop;
        //先将原来的checked保存
        let checkedArr = [...cartList];
        reqCartList({
            uid: uid,
        }).then(res => {
            if (res.data.code === 200) {
                // 请求成功时先将isreq打开
                dispatch(actions.changeIsReq(true));
                //确保有数据
                if (res.data.list) {
                    let list = res.data.list ? res.data.list[1].content : [];
                    list.forEach(ele => {
                        ele.checked = checkedArr.find(i => i.id === ele.id) ? checkedArr.find(i => i.id === ele.id).checked : false;
                    });
                    //修改全选
                    dispatch(actions.changeCheckedAll(!list.every(ele=>ele.checked)))
                    dispatch(actions.changeCartList(list))
                }
            }
        })
    },
    //删除操作
    reqCartDel: (id) => (dispatch, getState) => {
        reqCartDelete({ id }).then(res => {
            if (res.data.code === 200) {
                alert(res.data.msg);
                //刷新列表
                dispatch(actions.reqCartList());
            }
        })
    },
    //数量减少操作
    reqCut: (id, num) => (dispatch, getState) => {
        let { isReq } = getState().shop;
        //判断数量若是少于1则return
        if (num <= 1) return;
        //判断开关若为true则进行删除
        if (isReq) {
            //更改开关为false
            dispatch(actions.changeIsReq(false))
            reqcartedit({ id: id, type: 1 }).then(res => {
                if (res.data.code === 200) {
                    //刷新列表
                    dispatch(actions.reqCartList())
                }
            })
        }

    },
    //数量增加操作
    reqAdd: (id, num) => (dispatch, getState) => {
        if (num >= 10) return;
        reqcartedit({ id, type: 2 }).then(res => {
            if (res.data.code === 200) {
                //刷新列表
                dispatch(actions.reqCartList())
            }
        })
    },


};

//导出数据!
export const getCartList = (state) => state.shop.cartList;
//导出全选
export const getCheckedAll = (state) => state.shop.checkedAll;
//计算价格
export const getShowPrice = createSelector(
    [getCartList],
    (cartList) => {
        return cartList.reduce((val, ele) => ele.checked ? val += ele.num * ele.price : val, 0)
    }
)



//整体导出
export default reducer;

